using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Domain.Entities.Enums;
using Domain.Entities;
using UserInterface.Misc;
using Domain.Misc;
using Domain.Services;
using System.Collections.Generic;
using UserInterface.MML;

namespace UserInterface
{
    public partial class MessageHistory : BoardPageBase
    {
        private User requestedUser = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            int userId;
            if (int.TryParse(Request.QueryString["userId"], out userId))
            {
                requestedUser = UserService.GetById(userId);
            }

            if (requestedUser == null && Request.QueryString["user"] != null)
            {
                string username = Request.QueryString["user"];
                requestedUser = UserService.GetByUsername(username);
            }

            if (requestedUser == null)
            {
                requestedUser = CurrentSession.User;
            }

            if (!IsPostBack)
            {
                ShowMessageHistory();
            }
        }

        protected void ShowMessageHistory()
        {
            MessageHistoryPlaceHolder.Visible = true;
            UserNotFoundPlaceHolder.Visible = false;

            User user = CurrentSession.User;
            
            int messageCount = MessageService.GetMessageCountForUser(requestedUser);
            int messagesPerPage = PagingTools.GetMessagesPerPage(user);
            int pageCount = PagingTools.GetPageCount(messageCount, messagesPerPage);
            int currentPage = PagingTools.GetPage(Request.QueryString["page"], pageCount);

            IList<Message> messages = MessageService.GetMessagesPostedByUserPaged(requestedUser, currentPage, messagesPerPage);
            IList<Message> filteredMessages = PermissionManager.FilterReadableMessages(messages);
            MessageRepeater.DataSource = filteredMessages;
            MessageRepeater.DataBind();

            string urlBase = "MessageHistory.aspx?userId=" + requestedUser.Id;
            PagingTools.InitPagers(
                pageCount, currentPage, urlBase,
                TopPager, BottomPager
            );

            string pageTitle = (string)GetLocalResourceObject("Title");

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(pageTitle, "MessageHistory.aspx?userId=" + requestedUser.Id);

            UserInfoUsername.Text = requestedUser.Username;
            UserInfoDetailLiteral.Text = String.Format(
                (string)GetLocalResourceObject("UserInfoDetail"),
                messageCount,
                pageCount
            );

            if (filteredMessages.Count < messages.Count)
            {
                MasterPage.Messages.AddExclamation((string)GetLocalResourceObject("MessagesHiddenWarning"));
            }
        }

        private void ShowUserNotFound()
        {
            MessageHistoryPlaceHolder.Visible = false;
            UserNotFoundPlaceHolder.Visible = true;

            string pageTitle = (string)GetLocalResourceObject("Title");

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(pageTitle, "MessageHistory.aspx?user=" + UsernameTextBox.Text);
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            string username = UsernameTextBox.Text;
            User user = UserService.GetByUsername(username);
            if (user == null)
            {
                ShowUserNotFound();
            }
            else
            {
                requestedUser = user;
                ShowMessageHistory();
            }
        }

        protected string FilterText(Message message)
        {
            return Manager.Process(message.Text, message);
        }
    }
}
